#include "VoEMediaProcessImpl.h"


VoEMediaProcessImpl::VoEMediaProcessImpl(void)
{
	m_mid_Process = 0;
}


VoEMediaProcessImpl::~VoEMediaProcessImpl(void)
{
}

bool VoEMediaProcessImpl::init_jni(JNIEnv* env)
{
	bool result = false;

	do 
	{
		m_mid_Process = (env)->GetMethodID(m_cls, "process", "(ILcom/voe/common/ProcessingTypes;[SIZ)V");
		if(!m_mid_Process)
			break;

		m_cls_ProcessingTypes = (env)->FindClass("com/voe/common/ProcessingTypes");
		if(!m_cls_ProcessingTypes)
			break;

		m_mid_ProcessingTypes_fromValue = (env)->GetStaticMethodID(m_cls_ProcessingTypes, "fromValue", "(I)Lcom/voe/common/ProcessingTypes;");
		if(!m_mid_ProcessingTypes_fromValue)
			break;

		result = true;

	} while (false);

	return result;
}

void VoEMediaProcessImpl::Process(const int channel, const webrtc::ProcessingTypes type,
	WebRtc_Word16 audio10ms[], const int length,
	const int samplingFreq, const bool isStereo)
{
	JNIEnv* env = 0;

	if(before(&env))
	{
		jobject obj_type = (env)->CallStaticObjectMethod(m_cls_ProcessingTypes, m_mid_ProcessingTypes_fromValue);
		if(obj_type)
		{
			int len = length / sizeof(short);
			jshortArray array = JniHelper::c2j_shorts(env, audio10ms, len);
			(env)->CallVoidMethod(m_obj, m_mid_Process, 
				channel, obj_type, array, samplingFreq, isStereo);
			JniHelper::j2c_shorts(env, array, audio10ms, len);
		}

		after(env);
	}
}